package malus.shaofeng.com.beiwomusic.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.model.Comment;
import malus.shaofeng.com.beiwomusic.model.RefComment;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;

/**
 * 故事评论列表的适配器
 */
public class StoryCommendAdapter extends BaseAdapter {
    Context context;
    List<Comment> data;
    OnReplyClickListener listener;
    public StoryCommendAdapter(Context context,List<Comment> data){
        if(context!=null){
            this.context=context;
            this.data=data;
            if(context instanceof OnReplyClickListener){
                this.listener=(OnReplyClickListener)context;
            }else{
                throw new IllegalArgumentException("context must implment OnReplyClickListener");
            }
        }else{
            throw new IllegalArgumentException("context must not null");
        }
    }
    @Override
    public int getCount() {
        if(data!=null){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(data!=null){
            data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        if(convertView==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_story_comment_list,parent,false);
        }else{
            view=convertView;
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        if(holder==null){
            holder=new ViewHolder();
            holder.icon=(ImageView)view.findViewById(R.id.comment_item_icon);
            holder.nickname=(TextView)view.findViewById(R.id.comment_item_nickname);
            holder.title=(TextView)view.findViewById(R.id.comment_item_title);
            holder.reply=(TextView)view.findViewById(R.id.comment_item_reply);
            holder.reflayout=(LinearLayout)view.findViewById(R.id.comment_item_ll);
            holder.ref=(TextView)view.findViewById(R.id.comment_item_ref);
            view.setTag(holder);
        }
        Comment comment = data.get(position);
        holder.nickname.setText(comment.getNickname());
        holder.title.setText(comment.getContent());
        //设置头像图片
        if(comment.getUser()!=null){
            if(!TextUtils.isEmpty(comment.getUser().getFace_uri())){
                BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
                String imageUrl = BitmapHelper.getImageUrl(comment.getUser().getFace_uri());
                bitmapUtils.display(holder.icon, imageUrl,new BitmapLoadCallBack<ImageView>() {
                    @Override
                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                        int width = bitmap.getWidth();
                        Bitmap dst = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
                        Paint paint = new Paint();
                        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                        Canvas canvas = new Canvas(dst);
                        canvas.drawCircle(width / 2, width / 2, width / 2, paint);
                        imageView.setImageBitmap(dst);
                    }

                    @Override
                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

                    }
                });
            }
        }
        //设置回复的点击事件
        holder.reply.setTag(position);
        holder.reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!(v.getTag() instanceof Integer)){
                    return;
                }
                int pos=(int)v.getTag();
                if(listener!=null){
                    listener.onReplyClick(pos);
                }
            }
        });
        //设置回复的人
        RefComment ref_comment = comment.getRef_comment();
        if(ref_comment.getComment_id()!=0){
            holder.reflayout.setVisibility(View.VISIBLE);
            String refNickname = ref_comment.getUser().getNickname();
            String refContent = refNickname + "：" + ref_comment.getContent();
            SpannableStringBuilder builder = new SpannableStringBuilder(refContent);
            ForegroundColorSpan blueSpan=new ForegroundColorSpan(context.getResources().getColor(R.color.bbs_blue));
            builder.setSpan(blueSpan,0,refNickname.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.ref.setText(builder);
        }else{
            //没有回复
            holder.reflayout.setVisibility(View.GONE);
        }

        return view;
    }
    private class ViewHolder{
        TextView nickname;
        TextView title;
        TextView reply;
        ImageView icon;
        LinearLayout reflayout;
        TextView ref;
    }
    public interface OnReplyClickListener{
        void onReplyClick(int position);
    }
}
